package psd.model.Prolog.Converters;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.SuperFieldAccess;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologSuperFieldAccessConverter extends PrologConverter implements
IConverter<SuperFieldAccess>{

	public PrologSuperFieldAccessConverter(FactGenerationToolbox toolbox) {
		super(toolbox);
	}

	@Override
	public boolean convert(SuperFieldAccess node) {
		  String selectedFrom = null;
	        if(node.getQualifier() == null)
	        {
	            TypeDeclaration ultimateAncestor = getUltimateAncestor(node);
	            Type superclassType = ultimateAncestor.getSuperclassType();
	            String superClassID = superclassType != null ? idResolver.getID(superclassType.resolveBinding()) : idResolver.getJavaLangObjectID();
	            selectedFrom = idResolver.getID();
	            String identArgs[] = {
	                selectedFrom, idResolver.getID(node), idResolver.getID(getEnclosingNode(node)), "'super'", superClassID, this.getFilePath(node), this.getLineNumber(node)
	            };
	            writer.writeFact("identT", identArgs);
	            writer.writeFact("slT", new String[] {
	                selectedFrom, Integer.toString(node.getStartPosition()), Integer.toString(node.getLength()), this.getFilePath(node), this.getLineNumber(node)
	            });
	            checkAndGenComments(node, selectedFrom);
	        } else
	        {
	            ITypeBinding superclass = node.getQualifier().resolveTypeBinding().getSuperclass();
	            String superClassID = idResolver.getID(superclass);
	            selectedFrom = idResolver.getID();
	            String selectArgs[] = {
	                selectedFrom, idResolver.getID(node), idResolver.getID(getEnclosingNode(node)), "'super'", typeResolver.getTypeTerm((ITypeBinding)node.getQualifier().resolveBinding()), superClassID, this.getFilePath(node), this.getLineNumber(node)
	            };
	            writer.writeFact("selectT", selectArgs);
	            writer.writeFact("slT", new String[] {
	                idResolver.getID(node), Integer.toString(node.getStartPosition()), Integer.toString(node.getLength()), this.getFilePath(node), this.getLineNumber(node)
	            });
	            checkAndGenComments(node, selectedFrom);
	        }
	        String args[] = {
	            selectedFrom, quote(node.getName().getIdentifier()), idResolver.getID(node.resolveFieldBinding()), this.getFilePath(node), this.getLineNumber(node)
	        };
	        createBodyFact(node, "getFieldT", args);
	        writeSourceLocationArgumentIdentifier(node, node.getName());
	        return true;
	}

}
